home *** CD-ROM | disk | FTP | other *** search
/ Palm Utilities / Palm_Utilities_CD-ROM_2001_2001.iso / files / utils sync / todo2rtf 1.0 / todo2rtf.exe / Todo2rtf.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-02-04  |  8.1 KB  |  362 lines

  1. //
  2. // todo2rtf.cpp : Convert from todo database to RTF.
  3. //
  4.  
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <time.h>
  9.  
  10. // Struct...
  11. class Todo {
  12. public:
  13.     char *_str;
  14.     long int _date;
  15.     long int _completed;
  16.     long int _priority;
  17.     long int _category;
  18. }; 
  19.  
  20.  
  21. long int 
  22. GetLong(FILE *fp)
  23. {
  24.     // This will work on big-endian or small-endian...even if its
  25.     // a little slower...
  26.     unsigned long int ret;
  27.     unsigned char buf[4];
  28.  
  29.     buf[0] = getc(fp);
  30.     buf[1] = getc(fp);
  31.     buf[2] = getc(fp);
  32.     buf[3] = getc(fp);
  33.  
  34.     ret = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
  35.     return ret;
  36. }
  37.  
  38.  
  39. short int
  40. GetWord(FILE *fp)
  41. {
  42.     // This will work on big-endian or small-endian...even if its
  43.     // a little slower...
  44.     short int ret;
  45.     char buf[2];
  46.  
  47.     buf[0] = getc(fp);
  48.     buf[1] = getc(fp);
  49.  
  50.     ret = (buf[1] << 8) | buf[0];
  51.     return ret;
  52. }
  53.  
  54.  
  55. // 
  56. // NOTE: This returns a static pointer to a buffer, copy the string
  57. // or lose it...
  58. //
  59. char *
  60. GetString(FILE *fp)
  61. {
  62.     static char buf[20000];  // LOnger than longest!!!
  63.  
  64.     unsigned int len = getc(fp);
  65.  
  66.     if (len == 0)    
  67.         buf[0] = '\0';
  68.  
  69.  
  70.     if (len == 0xff) {
  71.         len = getc(fp);
  72.         len = len | (getc(fp) << 8);
  73.     }
  74.  
  75.     char *bp = buf;
  76.     for (int i=0; i < len; i++) 
  77.         *bp++ = getc(fp);
  78.  
  79.     *bp = '\0';
  80.     return buf;
  81. }
  82.  
  83. // I think that the max is 12...
  84. char *categories[32];
  85.  
  86.  
  87. // This is used to hold the order for categories when using the -r
  88. // option which arranges categories in the order I want.
  89. int rickmap[32];
  90. int rickorder = 0;
  91.  
  92.  
  93. int 
  94. main(int argc, char* argv[])
  95. {
  96.     int i;
  97.  
  98.     // Initialize the categories and rickmap to start with 0's
  99.     for (i=0; i < 32; i++) {
  100.         categories[i] = 0;
  101.         rickmap[i] = 0;
  102.     }
  103.  
  104.     // Category 0 is the unfiled category by definition
  105.     categories[0] = "Unfiled";
  106.     rickorder = 0;
  107.  
  108.     // First argument MUST be a file name of a todo file
  109.     if (argc < 2) {
  110.         printf("File to open required...cannot continue\n");
  111.         exit(2334);
  112.     }
  113.  
  114.     // Open the todo.dat file
  115.     FILE *fp = fopen(argv[1], "rb");
  116.     if (!fp) {
  117.         printf("Could not open file %s...cannot continue\n", argv[1]);
  118.         exit(2334);
  119.     }
  120.  
  121.     // Collect what flags are set
  122.     int printUnfiled = 0;
  123.     int min = 1, max = 5;
  124.     int textMode = 0;
  125.     char *fname = "todo.rtf";
  126.     for (i = 2; i < argc; i++) {
  127.         if (strcmp(argv[i], "-u") == 0) {
  128.             // Print unfiled category items
  129.             printUnfiled = 1;
  130.         } else if (strcmp(argv[i], "-r") == 0) {
  131.             // Use the rick order of categories
  132.             rickorder = 1;
  133.         } else if (strcmp(argv[i], "-t") == 0) {
  134.             // Print in text mode instead of RTF
  135.             textMode = 1;
  136.         } else if (strcmp(argv[i], "-h") == 0) {
  137.             // Set the maximum priority todo to print
  138.             max = atoi(argv[i+1]);
  139.             i++;
  140.         } else if (strcmp(argv[i], "-l") == 0) {
  141.             // Set the minimum priority todo to print
  142.             min = atoi(argv[i+1]);
  143.             i++;
  144.         } else if (strcmp(argv[i], "-p") == 0) {
  145.             // Set the min/max priority todo to print the same
  146.             min = max = atoi(argv[i+1]);
  147.             i++;
  148.         } else if (strcmp(argv[i], "-o") == 0) {
  149.             // Use a different output file
  150.             fname = argv[i+1];
  151.             i++;
  152.         } else {
  153.             // I dont know this flag
  154.             printf("Unknown flag %s ignored\n", argv[i]);
  155.         }
  156.     }
  157.  
  158.     // Read through input section
  159.     GetLong(fp);  
  160.     GetString(fp);
  161.     GetString(fp);
  162.     GetLong(fp);
  163.  
  164.     // Get the number of categories
  165.     int numCats = GetLong(fp);
  166.  
  167.     // Where to start putting categories based on the 
  168.     // rickorder. If we are doing rickorder then start at 4 because I
  169.     // have 4 pre-sorted categories (ie: I want these guys first).
  170.     // If you change the categories make sure and update this number
  171.     int indexStart = 0;
  172.     if (rickorder)
  173.         indexStart = 4;
  174.  
  175.     // Go through the categories and put them in the map which keeps track
  176.     // of their order
  177.     for (i = 0; i < numCats; i++) {
  178.         int index = GetLong(fp);
  179.         GetLong(fp);
  180.         GetLong(fp);
  181.         char *cat = GetString(fp);     // Keep the long name
  182.  
  183.         // printf("Category: index: %d str: %s\n", index, cat);
  184.  
  185.         categories[index] = new char [strlen(cat) + 1];
  186.         strcpy(categories[index], cat);
  187.  
  188.         // If -r is set then I want the first four categories to be
  189.         // Today, work, home, and programs.  After that I don't care
  190.         // what order they come in
  191.         if (rickorder) {
  192.             if (strcmp(cat, "Today") == 0) 
  193.                 rickmap[0] = index;
  194.             else if (strcmp(cat, "Work") == 0) 
  195.                 rickmap[1] = index;
  196.             else if (strcmp(cat, "Home") == 0) 
  197.                 rickmap[2] = index;
  198.             else if (strcmp(cat, "Programs") == 0) 
  199.                 rickmap[3] = index;
  200.             else 
  201.                 rickmap[indexStart++] = index;
  202.         } else {
  203.             // If not in rick order this will just fill up by index
  204.             rickmap[indexStart++] = index;
  205.         }
  206.         GetString(fp);                 // Burn the short name
  207.     }
  208.  
  209.     // printf("cat order\n");
  210.     // for (i = 0; i < numCats; i++) {
  211.     //     printf("%d == %s\n", rickmap[i], categories[rickmap[i]]);
  212.     // }
  213.  
  214.     // Now look at the table-description (whatever that is...)
  215.     GetLong(fp);
  216.     int numFields = GetLong(fp);
  217.     GetLong(fp);    
  218.     GetLong(fp);    
  219.     GetLong(fp);
  220.     GetWord(fp);
  221.  
  222.     for (i = 0; i < numFields; i++) {
  223.         GetWord(fp);
  224.     }
  225.  
  226.     int numTodos = GetLong(fp);
  227.  
  228.     // For some martian reason, this is *10...
  229.     numTodos = numTodos / 10;
  230.  
  231.     // printf("Number of ToDo's: %d\n", numTodos);
  232.  
  233.     // Allocate data structures...and a spare for fun
  234.     Todo *todos = new Todo[numTodos + 1];
  235.  
  236.     for (i=0; i < numTodos; i++) {
  237.  
  238.         GetLong(fp);      
  239.         GetLong(fp);    
  240.  
  241.         GetLong(fp);  
  242.         GetLong(fp);
  243.  
  244.         GetLong(fp);  
  245.         GetLong(fp);
  246.  
  247.         GetLong(fp);  
  248.         GetLong(fp);  
  249.  
  250.         char *s = GetString(fp);
  251.         todos[i]._str = new char [strlen(s) + 1];
  252.         strcpy(todos[i]._str, s);
  253.  
  254.         GetLong(fp);  // 3 = date
  255.         todos[i]._date = GetLong(fp);
  256.  
  257.         GetLong(fp);  // 6 = Bool
  258.         todos[i]._completed = GetLong(fp);
  259.  
  260.         GetLong(fp);  // 1 = int
  261.         todos[i]._priority = GetLong(fp);
  262.  
  263.         GetLong(fp);  
  264.         GetLong(fp);  
  265.  
  266.         GetLong(fp);  // 1 = int
  267.         todos[i]._category = GetLong(fp);
  268.  
  269.         GetLong(fp);  // 5 = Cstr
  270.         GetLong(fp);  // just a 0
  271.  
  272.         GetString(fp);  // Eat the note!
  273.     }
  274.  
  275.  
  276.     // Get output file ready to go
  277.     FILE *fout = fopen(fname, "w");
  278.     if (!fout) {
  279.         printf("Could not open file %s...cannot continue\n", fname);
  280.         exit(2335);
  281.     }
  282.  
  283.     if (!textMode) {
  284.         fprintf(fout, "{\\rtf1\\ansi\\deff0\\deftab720{\\fonttbl{\\f0\\fswiss\\fprq2 Arial;}}");
  285.         fprintf(fout, "\\deflang1033\\pard\\plain\\f0\\fs20");
  286.     }
  287.  
  288.     // Print by category, and by priority
  289.     // Go through the different categories
  290.     int p;
  291.     char dbuf[256];
  292.     char *dptr;
  293.     for (i = 0; i <= numCats; i++) {
  294.  
  295.         int c = rickmap[i];
  296.  
  297.         // Skip if this is the unfiled category and don't want to print it
  298.         if (!printUnfiled && (c == 0))
  299.             continue;
  300.  
  301.         int anyInCat = 0;
  302.  
  303.         // Print stuff in priority order within categories
  304.         for (p = min; p <= max; p++) {
  305.  
  306.             for (int j = 0; j < numTodos; j++) {
  307.  
  308.                 // If priority matches what we are looking at and in the right category then
  309.                 // go to town;
  310.                 if ((todos[j]._priority == p) && (todos[j]._category == c)) {
  311.  
  312.                     // IF this is the first printing for this category then print
  313.                     // a header
  314.                     if (!anyInCat) {
  315.                         if (!textMode)
  316.                             fprintf(fout, "\\par\\par\\plain\\f0\\b\\ul\\fs24 %s\n", 
  317.                                     categories[c]); // bold big
  318.                         else {
  319.                             fprintf(fout, "\n%s\n", categories[c]); 
  320.                             fprintf(fout, "--------------------\n", categories[c]); 
  321.                         }
  322.  
  323.                         anyInCat = 1;
  324.                     }
  325.  
  326.                     // If the item is not completed prefix with an o, else an x
  327.                     char comp = 'o';
  328.                     if (todos[j]._completed)
  329.                         comp = 'x';
  330.  
  331.                     time_t bob = todos[j]._date;
  332.                     struct tm *nt;
  333.                     nt = localtime(&bob);
  334.                     // NOTE: Check out the +1 for month, +1900 for year!!!
  335.                     sprintf(dbuf, " - (%d/%d/%d)", nt->tm_mon+1, nt->tm_mday, nt->tm_year+1900);
  336.  
  337.                     // No date if past date 2030:
  338.                     if (nt->tm_year > 130) 
  339.                         dptr = " ";
  340.                     else
  341.                         dptr = dbuf;
  342.                     
  343.                     if (!textMode) {
  344.                         fprintf(fout, "\\par\\plain\\f0\\fs20\\b %c \\b0 %d: %s %s\n", 
  345.                                 comp, p, todos[j]._str, dptr); 
  346.                     } else {
  347.                         fprintf(fout, "%c %d: %s %s\n",    comp, p, todos[j]._str, dptr); 
  348.                     }
  349.                 }
  350.             }
  351.         }
  352.     }
  353.  
  354.     if (!textMode)
  355.         fprintf(fout, "\\par }\n");
  356.  
  357.     fclose(fp);
  358.     fclose(fout);
  359.     return 0;
  360. }
  361.  
  362.